<?php

require_once("config.php");
require_once("nusoap/nusoap.php");
require_once("utils.php");
require_once("Component.php");

class Contact extends Component {

	var $defaultColumns;
	var $columnMappings;
	
        function __construct($proxy) {

		$this->defaultColumns = array(
                                        'salutation',
                                        'first_name',
                                        'last_name',
                                        'account_id',
                                        'lead_source',
                                        'campaign_id',
                                        'title',
                                        'department',
                                        'assigned_user_id',
                                        'email1',
                                        'phone_work',
                                        'phone_mobile',
                                        'phone_home',
                                        'phone_other',
                                        'phone_fax',
                                        'do_not_call',
                                        'primary_address_street',
                                        'primary_address_city',
                                        'primary_address_state',
                                        'primary_address_postalcode',
                                        'primary_address_country',
                                        'description',
                                        );
		
		$this->columnMappings = array("contacts.account_id" => "accounts_contacts.account_id");

		
		parent::__construct("Contacts", "contacts", $proxy);
	}

	function getRecords() {

		$customQuery = null;

                if ($this->keysOnly == null) {

			$query = $this->getQueryColumns($this->defaultColumns);
                        $query = str_replace('contacts.email1','email_address email1', $query);
			$query = str_replace('contacts.account_id','accounts_contacts.account_id', $query);
                        $query = 'select '.$query.' from contacts ';
                        $query .=  ' LEFT JOIN  email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Contacts\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                        $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
			$query .= ' LEFT JOIN accounts_contacts ON accounts_contacts.contact_id = contacts.id ';
			$query .= '                             AND accounts_contacts.deleted = 0';

			$account_id = isset($_REQUEST['account_id']) ? $_REQUEST['account_id'] : null;

                        $customQuery =  ($account_id != null) ? " accounts_contacts.account_id=".$this->quote.$account_id.$this->quote : null;


		} else {
			$query = "select id from contacts";
		}


		#$columnMappings = array("contacts.account_id" => "accounts_contacts.account_id");

		#foreach ($columnMappings as $column => $value) {
		#	echo $column." = ".$columnMappings[$column];
		#}

		return $this->getRows($query, $customQuery, " contacts.date_modified desc ", $this->columnMappings);
        }


	function getRecord($condition) {

		$query = $this->getQueryColumns($this->defaultColumns);
                $query = str_replace('contacts.email1','email_address email1', $query);
                $query = str_replace('contacts.account_id','accounts_contacts.account_id', $query);
                $query = 'select '.$query.' from contacts ';
                $query .=  ' LEFT JOIN  email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Contacts\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
                $query .= ' LEFT JOIN accounts_contacts ON accounts_contacts.contact_id = contacts.id ';
		$query .= '                             AND accounts_contacts.deleted = 0';
		$query .= ' WHERE '.$condition;

		return $this->getRow($query);

        }


	function getRecordCount() {

		$query = 'select count(*) row_count from contacts ';
		$query .=  ' LEFT JOIN  email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Contacts\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
                $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
                $query .= ' LEFT JOIN accounts_contacts ON accounts_contacts.contact_id = contacts.id ';
		$query .= '                             AND accounts_contacts.deleted = 0';

		$account_id = isset($_REQUEST['account_id']) ? $_REQUEST['account_id'] : null;
                $customQuery =  ($account_id != null) ? " accounts_contacts.account_id=".$this->quote.$account_id.$this->quote : null;

                return $this->getTotalRowCount($query, $customQuery, $this->columnMappings);
        }


	function saveRecord($id, $photo_c, $salutation, $first_name, $last_name,
 						     $account_id, $lead_source, $campaign_id,
							 $title, $department, $assigned_user_id,
							 $email1, $phone_work, $phone_mobile, $phone_home, $phone_other,
							 $phone_fax, $do_not_call, $primary_address_street,
							 $primary_address_city, $primary_address_state,
							 $primary_address_postalcode, 
							 $primary_address_country, $description,
							 $date_modified) {
		
		
			if ($id) {
				$response = $this->proxy->set_entry($this->sess, 'Contacts', 
								array(array("name" => 'id',"value" => $id),
									  array("name" => 'salutation',"value" => $salutation),
								      	array("name" => 'first_name',"value" => $first_name),
            					      			array("name" => 'last_name',"value" => $last_name),
									  array("name" => 'account_id',"value" => $account_id),
									  array("name" => 'lead_source',"value" => $lead_source),
									  array("name" => 'campaign_id',"value" => $campaign_id),
									  array("name" => 'title',"value" => $title),
									  array("name" => 'department',"value" => $department),
									  array("name" => 'assigned_user_id',"value" => $assigned_user_id),
									  array("name" => 'email1',"value" => $email1),
									  array("name" => 'phone_work',"value" => $phone_work),
									  array("name" => 'phone_mobile',"value" => $phone_mobile),
									  array("name" => 'phone_home',"value" => $phone_home),
									  array("name" => 'phone_other',"value" => $phone_other),
									  array("name" => 'phone_fax',"value" => $phone_fax),
									  array("name" => 'do_not_call',"value" => $do_not_call),
									  array("name" => 'primary_address_street',"value" => $primary_address_street),
									  array("name" => 'primary_address_city',"value" => $primary_address_city),
									  array("name" => 'primary_address_state',"value" => $primary_address_state),
									  array("name" => 'primary_address_postalcode',"value" => $primary_address_postalcode),
									  array("name" => 'primary_address_country',"value" => $primary_address_country),
            						  		array("name" => 'description',"value" => $description),
            					      			array("name" => 'date_modified',"value" => $date_modified)
            						 )
            				);
			} else {
				$response = $this->proxy->set_entry($this->sess, 'Contacts', 
								array(
								      array("name" => 'salutation',"value" => $salutation),
								      array("name" => 'first_name',"value" => $first_name),
            					      array("name" => 'last_name',"value" => $last_name),
									  array("name" => 'account_id',"value" => $account_id),
									  array("name" => 'lead_source',"value" => $lead_source),
									  array("name" => 'campaign_id',"value" => $campaign_id),
									  array("name" => 'title',"value" => $title),
									  array("name" => 'department',"value" => $department),
									  array("name" => 'assigned_user_id',"value" => $assigned_user_id),
									  array("name" => 'email1',"value" => $email1),
									  array("name" => 'phone_work',"value" => $phone_work),
									  array("name" => 'phone_mobile',"value" => $phone_mobile),
									  array("name" => 'phone_home',"value" => $phone_home),
									  array("name" => 'phone_other',"value" => $phone_other),
									  array("name" => 'phone_fax',"value" => $phone_fax),
									  array("name" => 'do_not_call',"value" => $do_not_call),
									  array("name" => 'primary_address_street',"value" => $primary_address_street),
									  array("name" => 'primary_address_city',"value" => $primary_address_city),
									  array("name" => 'primary_address_state',"value" => $primary_address_state),
									  array("name" => 'primary_address_postalcode',"value" => $primary_address_postalcode),
									  array("name" => 'primary_address_country',"value" => $primary_address_country),
            						  array("name" => 'description',"value" => $description),
            					      array("name" => 'date_modified',"value" => $date_modified)
									  )
            			);
            		}				 

			## If new account is associated, set the prvious account to deleted in accounts_contacts
			return $response;
		}
		
		
	}
	
$component = new Contact($proxy);

$salutationsArray = array("" => "", 
 						  "Mr." => "Mr.", 
						  "Ms." => "Ms.", 
						  "Mrs." => "Mrs.", 
						  "Dr." => "Dr.", 
						  "Prof." => "Prof.");
						  
$leadsArray = array("" => "",
				    "Cold Call" => "Cold Call",
					"Existing Customer" => "Existing Customer",
					"Self Generated" => "Self Generated",
					"Employee" => "Employee",
					"Partner" => "Partner",
					"Public Relations" => "Public Relations",
					"Direct Mail" => "Direct Mail",
					"Conference" => "Conference",
					"Trade Show" => "Trade Show",
					"Web Site" => "Web Site",
					"Word of mouth" => "Word of mouth",
					"Email" => "Email",
					"Campaign" => "Campaign",
					"Other" => "Other");
?>
